package org.example.utils;

import cn.hutool.core.util.StrUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.example.entity.dto.UserDto;

public class SessionUtils {

    public static String getTokenFromRequest(HttpServletRequest request){
        String token = request.getParameter("token");
        if (StrUtil.isBlank(token)){
            token = request.getHeader("token");
        }
        return token;
    }
    public static UserDto getUserFromSession(HttpServletRequest request) {
        // 从请求中获取token
        String token = getTokenFromRequest(request);
        // 获取请求的session
        HttpSession session = request.getSession();
        //从session中获取用户信息
        UserDto userInfo = (UserDto)session.getAttribute(token);
        return userInfo;
    }
}
